package org.hepeng.workx.spring.cloud.netflix.eureka.client;

import com.netflix.appinfo.InstanceInfo;
import org.apache.commons.lang3.StringUtils;
import org.hepeng.workx.service.RestServiceCallResult;
import org.hepeng.workx.service.ServiceError;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author he peng
 */

@RestController
@RequestMapping("/eureka-client")
public class EurekaClientController {

    @RequestMapping("/{status}")
    public ResponseEntity<RestServiceCallResult> status(@PathVariable("status") String status) {
        InstanceInfo.InstanceStatus instanceStatus = InstanceInfo.InstanceStatus.toEnum(status);
        if (! StringUtils.equals(StringUtils.upperCase(status) , instanceStatus.name())) {
            RestServiceCallResult callResult =
                    RestServiceCallResult.newRestServiceCallResult(
                            ServiceError.PARAMETER_ERROR,
                            status + " does not belong to InstanceStatus");
            return ResponseEntity.badRequest().body(callResult);
        }

        EurekaDiscoveryClientUtils.status(InstanceInfo.InstanceStatus.toEnum(status));
        RestServiceCallResult callResult = RestServiceCallResult.newRestServiceCallResult(ServiceError.SUCCESS);
        return ResponseEntity.ok(callResult);
    }


}
